package com.mvc.demo.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Locale;

/**
 * @author H
 * @date 2023/1/8
 */
@RestController
@RequestMapping(path = "/i18n")
public class TestI18nController {

    /**
     * 注入国际化消息接口对象
     */
    @Autowired
    private MessageSource messageSource;

    /**
     * 后台获取国际化信息
     * @return
     */
    @GetMapping("test")
    public String test() {
        // 后台获取国际化区域
        Locale locale = LocaleContextHolder.getLocale();
        // 获取国际化消息
        String msg = messageSource.getMessage("i18n_msg_test", null, locale);
        return msg;
    }
}
